const Actions = require('./actions');
const DB = require('app/db.js');
import { Toast } from 'saltui';
import { Control } from 'react-keeper';
import { monStorage, Storage } from 'clientConfig/util/StoreData';
module.exports = Reflux.createStore({
    listenables: [Actions],
    data: {
    },

    onFetch: function (params, cb) {
        let t = this;
    },
    onGetOrgListInfo: function (params, cb) {
        let t = this;
        DB.Notice.get_org_lists(
            {
                schoolId: Storage.get('user.schoolId'),
                userRole: Storage.get('user.type')==0?'T':'P',
                userName: Storage.get('user.talkId')
            }
        ).then((content) => {
            t.data.listData = content.data.dataList;
            // monStorage.set('org_list', content.data.dataList);
            t.updateComponent()
        }).catch((error) => { // 失败 or 有异常被捕获
            Toast.hide(() => {
            });
            Toast.show({
                type: 'error',
                content: '系统异常'
            });
        })
    },
    onDelivernotice: function (params, cb) {
        let t = this;
        for (let item of params.users) {
            item.memeberId = item.memberId;
            // delete item.memberId;
        }
        let userdata = JSON.stringify({ 'noticeMember': params.users });
        let config = {
            replayFlag:params.reply ? 1 : 0,
            regularlyFlag:params.value ? 1 : 0,
            regularlyDate:params.value ? params.value : null,
            releaseName:params.signature,
            schoolId: Storage.get('user.schoolId'),
            teacherId: Storage.get('user.teacherId'),
            noticeSubject: params.title,
            noticeContent: params.content,
            noticePublish: Storage.get('user.name'),
            data: userdata
        };
        let url = "/app/notice/teacher/sendNoticeWithImg?replayFlag=" + config.replayFlag + "&regularlyFlag=" + config.regularlyFlag + "&regularlyDate="+config.regularlyDate+ "&releaseName=" + config.releaseName + "&schoolId=" + config.schoolId + "&teacherId=" + config.teacherId + "&noticeSubject=" + config.noticeSubject + "&noticeContent=" + config.noticeContent + "&noticePublish=" + config.noticePublish + "&data=" + config.data;
        Toast.show({
            type: 'loading',
            content: '发布中',
            hasMask: true,
            duration: 300000000,
        });
        fetch(url, {
            method: "POST",
            body: params.formData
        }).then(function (response) {
             return response.json();
        }).then(function (result) {
            Toast.hide(() => {
            });
            if (result.content.code == 1) {                
                Toast.show({
                    type: 'success',
                    content: '发布成功'
                });
                Control.go(-1);
            }
        }, function (error) {
            Toast.hide(() => {
            });
            Toast.show({
                type: 'error',
                content: '系统异常'
            });
        })
    },
    updateComponent: function () {
        this.trigger(this.data);
    }
});
